package cn.pegasus.teamwork.pegasus.service.impl;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageHelper;
import cn.iyizhan.teamwork.base.util.PageObject;
import cn.pegasus.teamwork.pegasus.entity.BussGeneralAccount;
import cn.pegasus.teamwork.pegasus.entity.BussPropertyFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussPropertyInfo;
import cn.pegasus.teamwork.pegasus.entity.BussUserOrder;
import cn.pegasus.teamwork.pegasus.mapper.BussGeneralAccountMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussPropertyFunddetailsMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussPropertyInfoMapper;
import cn.pegasus.teamwork.pegasus.mapper.BussUserOrderMapper;
import cn.pegasus.teamwork.pegasus.service.BussPropertyInfoService;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigDictionary;
import cn.pegasus.teamwork.sysconfig.mapper.SysConfigDictionaryMapper;
import cn.pegasus.teamwork.util.DateCompareUtil;


/**
 * @author cyl
 * @version 
 */
@Service("bussPropertyInfoService")
public class BussPropertyInfoServiceImpl extends BaseServiceImpl<BussPropertyInfo, BussPropertyInfoMapper> implements BussPropertyInfoService {
	// 注入当前dao对象
    @Autowired
    private BussPropertyInfoMapper bussPropertyInfoMapper;
    @Autowired
	private BussUcenterAreaService ucenterAreaService;
    @Autowired
    private BussPropertyFunddetailsMapper propertyFunddetailsMapper;
    @Autowired
	private SysConfigDictionaryMapper sysConfigDictionaryMapper;
    @Autowired
	private BussUserOrderMapper bussUserOrderMapper;
    @Autowired
    private BussGeneralAccountMapper generalAccountMapper;
    public BussPropertyInfoServiceImpl() {
        setMapperClass(BussPropertyInfoMapper.class, BussPropertyInfo.class);
    }

	@Override
	public PageObject<BussPropertyInfo> PageList(Map<String, Object> map) {
		map.put("sortName", "create_time");
        map.put("sortOrder", "desc");
        int totalData = bussPropertyInfoMapper.getCount(map);
        PageHelper pageHelper = new PageHelper(totalData, map);
        List<BussPropertyInfo> list = bussPropertyInfoMapper.pageQueryByObject(pageHelper.getMap());
        if(null != list && list.size() > 0){
        	for(BussPropertyInfo p : list){
            	if(p.getAreaId()!=null){
            		String name = ucenterAreaService.getAreaName(p.getAreaId());
            		p.setAreaName(name);
            	}
            }
        }
        PageObject<BussPropertyInfo> pageObject = pageHelper.getPageObject();
        pageObject.setDataList(list);
		return pageObject;
	}

	@Override
	public void addProperty(Long userId, Map<String, Object> map) {
		BussPropertyInfo info = M2O(map);
		info.setPropertyStatus(0);
		info.setCreateTime(new Date());
		info.setCreateUserId(userId);
		info.setBalance(0L);
		info.setExBalance(0L);
		bussPropertyInfoMapper.insertByEntity(info);
	}

	@Override
	public void updateProperty(Long userId, Map<String, Object> map) {
		BussPropertyInfo info = M2O(map);
		bussPropertyInfoMapper.updateByEntity(info);
		
	}

	@Override
	public void deleteProperty(Long userId, Long id) {
		BussPropertyInfo info = new BussPropertyInfo();
		info.setPropertyStatus(1);
		info.setId(id);
		bussPropertyInfoMapper.updateByEntity(info);
	}

	@Override
	public BussPropertyInfo findPropertyInfo(Long id) {
		BussPropertyInfo info = new BussPropertyInfo();
		info.setId(id);
		return bussPropertyInfoMapper.selectSingle(info);
	}

	@Override
	public void drawing(Long id, double balance) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		long b = (long)balance*100;
		map.put("balance", b);
		bussPropertyInfoMapper.updateProperyInfo(map);
	}

	@Override
	public void play(Long userId,Long id, Long balance) {
		//修改物业提款金额
		bussPropertyInfoMapper.paly(id);
		//生成订单
		BussPropertyFunddetails f = new BussPropertyFunddetails();
		f.setCreateTime(new Date());
		f.setBalance(balance);
		f.setPropertyId(id);
		f.setType(2);
		f.setCreateUserId(userId);
		propertyFunddetailsMapper.insertByEntity(f);
	}

	//定时任务物业结算
	@Override
	public void propertySettlement() throws Exception{
		// 查询配置时间settlementTime
		SysConfigDictionary configTime = new SysConfigDictionary();
		configTime.setDictName("settlementTime");
		SysConfigDictionary cTime = sysConfigDictionaryMapper.selectSingle(configTime);
		String value = cTime.getDictValue();
		if (value == null) {
			return;
		}
		int day = Integer.valueOf(DateCompareUtil.getDateString("dd"));
		Integer ss = Integer.valueOf(value);
		if(ss != day){
			return;
		}
		BussGeneralAccount a = new BussGeneralAccount();
		//按单算
		List<String> lst = DateCompareUtil.getDatStartAndEndDay();
		this.byNum(lst,a);
		//按月算
		this.byMonth(lst,a);
		//刷新总单
		this.account(lst,a);
	}
	//统计总账单
	public void account(List<String> lst,BussGeneralAccount a){
		a.setCreateTime(new Date());
		a.setEndTime(lst.get(1));
		a.setStartTime(lst.get(0));
		generalAccountMapper.insertByEntity(a);
	}
	//按月固定额结算
	public void byMonth(List<String> lst,BussGeneralAccount a){
		BussPropertyInfo info = new BussPropertyInfo();
		info.setPropertyStatus(0);
		info.setPropertyProportions(2);
		long propertyBalance = a.getPropertyBalance();
		List<BussPropertyInfo> list = bussPropertyInfoMapper.selectByObject(info);
		if(null != list && list.size() > 0){
			//按固定额
			SysConfigDictionary con = new SysConfigDictionary();
			con.setDictName("byMonth");
			SysConfigDictionary c = sysConfigDictionaryMapper.selectSingle(con);
			if(null != c){
				Long money = (long) (Double.valueOf(c.getDictPvalue())*100);
				for(BussPropertyInfo property : list){
					//查询订单数据
					Map<String,Object> params = new HashMap<String,Object>();
					params.put("end", lst.get(1));
					params.put("start", lst.get(0));
					params.put("areaId", property.getAreaId());
					List<BussUserOrder> orderList = bussUserOrderMapper.findBussUserOrderList(params);
					if(null != orderList && orderList.size() > 0){
						int successNUm = 0;
						int refundNum = 0;
						Long successB = 0L;
						Long refundB = 0L;
						for(BussUserOrder o : orderList){
							if(o.getOrderStatus() == 3){
								successNUm += 1;
								successB = successB + o.getRechargeBalance();
							}
							if(o.getOrderStatus() == 21){
								refundNum += 1;
								refundB = refundB + o.getRechargeBalance();
							}
						}
						propertyBalance = propertyBalance + money;
						//保存物业订单
						BussPropertyFunddetails f = new BussPropertyFunddetails();
						f.setBalance(money);
						f.setCreateTime(new Date());
						f.setEndTime(lst.get(1));
						f.setOrderId(successNUm);
						f.setPropertyId(property.getId());
						f.setRefundBalance(refundB);
						f.setRefundNum(refundNum);
						f.setStartTime(lst.get(0));
						f.setSuccesBalance(successB);
						f.setType(1);
						propertyFunddetailsMapper.insertByEntity(f);
					}
				}
			}
			a.setPropertyBalance(propertyBalance);
		}
	}
	//按数量统计
	public void byNum(List<String> lst,BussGeneralAccount a){
		BussPropertyInfo info = new BussPropertyInfo();
		info.setPropertyStatus(0);
		info.setPropertyProportions(1);
		long propertyBalance = 0L;
		List<BussPropertyInfo> list = bussPropertyInfoMapper.selectByObject(info);
		if(null != list && list.size() > 0){
			//按月比例分配
			SysConfigDictionary con = new SysConfigDictionary();
			con.setDictName("byNumber");
			SysConfigDictionary c = sysConfigDictionaryMapper.selectSingle(con);
			if(null != c){
				Long money = (long) (Double.valueOf(c.getDictPvalue())*100);
				for(BussPropertyInfo property : list){
					//查询订单数据
					Map<String,Object> params = new HashMap<String,Object>();
					params.put("end", lst.get(1));
					params.put("start", lst.get(0));
					params.put("areaId", property.getAreaId());
					List<BussUserOrder> orderList = bussUserOrderMapper.findBussUserOrderList(params);
					if(null != orderList && orderList.size() > 0){
						int successNUm = 0;
						int refundNum = 0;
						Long successB = 0L;
						Long refundB = 0L;
						for(BussUserOrder o : orderList){
							if(o.getOrderStatus() == 3){
								successNUm += 1;
								successB = successB + o.getRechargeBalance();
							}
							if(o.getOrderStatus() == 21){
								refundNum += 1;
								refundB = refundB + o.getRechargeBalance();
							}
						}
						long balance = successNUm*money;
						propertyBalance = propertyBalance + balance;
						//保存物业订单
						BussPropertyFunddetails f = new BussPropertyFunddetails();
						f.setBalance(balance);
						f.setCreateTime(new Date());
						f.setEndTime(lst.get(1));
						f.setOrderId(successNUm);
						f.setPropertyId(property.getId());
						f.setRefundBalance(refundB);
						f.setRefundNum(refundNum);
						f.setStartTime(lst.get(0));
						f.setSuccesBalance(successB);
						f.setType(1);
						propertyFunddetailsMapper.insertByEntity(f);
					}
				}
			}
		}
		a.setPropertyBalance(propertyBalance);
	}

	@Override
	public PageObject<BussPropertyInfo> PageListAdu(Map<String, Object> map) {
		map.put("sortName", "create_time");
        map.put("sortOrder", "desc");
        int totalData = bussPropertyInfoMapper.total(map);
        PageHelper pageHelper = new PageHelper(totalData, map);
        List<BussPropertyInfo> list = bussPropertyInfoMapper.findList(pageHelper.getMap());
        if(null != list && list.size() > 0){
        	for(BussPropertyInfo p : list){
            	if(p.getAreaId()!=null){
            		String name = ucenterAreaService.getAreaName(p.getAreaId());
            		p.setAreaName(name);
            	}
            }
        }
        PageObject<BussPropertyInfo> pageObject = pageHelper.getPageObject();
        pageObject.setDataList(list);
		return pageObject;
	}
}
